package base.join;

/**
 * @author chengzhang
 * 2021/8/24 19:13
 */
public class Join02 {

    /**
     * 业务场景: 等待某几件事完成后才继续往下执行，如多个线程加载资源，需要等待多个线程全部加载完毕再汇总处理
     */
    public static void main(String[] args) throws InterruptedException {
        //主线程在调用thread01.join()方法后被阻塞，等待主线程在调用thread01执行完毕后返回
        //主线程在调用thread02.join()方法后被阻塞，等待主线程在调用thread02执行完毕后返回
        Thread thread01 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("child thread01 over");
        });

        Thread thread02 = new Thread(() -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("child thread02 over");

        });

        //启动子线程
        thread01.start();
        thread02.start();

        //等待子线程执行完毕，返回
        thread01.join();
        thread02.join();
    }


}
